package com.gongpingjialoan.activity.loans;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.adapter.LoanHistoryAdapter;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.bean.LoabHistoryBean;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.LoanUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class LoanHistoryActivity extends BaseActivity implements NetDataJson.OnStringListener {

    private ListView mListView;

    private LinearLayout nodataImageView;

    private NetDataJson mNetDataJson;

    private LoanHistoryAdapter mLoanRecordAdapter;

    private Button loan_button;

    private List<LoabHistoryBean.DataBean> mRecords;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSendData = true;
        isScrollPage = true;
        action = "my_loanRecord";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loan_history);
        setTitle("贷款记录");
        nodataImageView = (LinearLayout) findViewById(R.id.no_info);
        mListView = (ListView) findViewById(R.id.listview);
        loan_button = (Button) findViewById(R.id.loan_button);
//        String city = Utils.getCityName(getApplicationContext());
//        if(("苏州".equals(city) || "成都".equals(city))){
//            loan_button.setVisibility(View.VISIBLE);
//        }else{
//            loan_button.setVisibility(View.GONE);
//        }
//        loan_button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                getOrderId();
//            }
//        });
        mRecords = new ArrayList<>();
        mLoanRecordAdapter = new LoanHistoryAdapter(mRecords, this);
        mListView.setAdapter(mLoanRecordAdapter);
        mNetDataJson = new NetDataJson(this,1);
        mNetDataJson.setUrl(NetDataJson.LOAN_SERVER_DOMAIN  + API.loan_histories);
        mNetDataJson.setUseOtherDomain(true);
        mNetDataJson.request("get");

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LoabHistoryBean.DataBean source = (LoabHistoryBean.DataBean) mLoanRecordAdapter.getItem(position);
                if ("消费贷".equals(source.getKind())){
                    getLoanStatus();
                }
            }
        });
    }

//    private void getOrderId() {
//        NetDataJson netDataJson = new NetDataJson(new NetDataJson.OnStringListener() {
//            @Override
//            public void onDataJsonError(String errorMessage) {
//                Toast.makeText(mySelf, errorMessage, Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onDataJsonUpdate(String json) {
//                Gson gson = new Gson();
//                LoanOrderData loanOrderData = gson.fromJson(json, LoanOrderData.class);
//                String order_id = loanOrderData.getData().getOrder_no();
//                String user_id = loanOrderData.getData().getOrder_no();
//
//                gotoLoanPage(order_id,user_id);
//
//            }
//        }, 1);
//        netDataJson.setUrl(NetDataJson.LOAN_SERVER_DOMAIN + API.loan_get_order_id);
//        netDataJson.setUseOtherDomain(true);
//        netDataJson.addParam("source", 2);
//        netDataJson.request("post");
//    }

//    private void gotoLoanPage(String order_id,String user_id) {
//        Intent intent = new Intent(this, WebViewActivity.class);
//        String url = "http://www.eflashloan.com/eflashapp/index.html#" + order_id;
//
//        intent.putExtra("url", url);
//        intent.putExtra("order_id", order_id);
//        intent.putExtra("user_id", user_id);
//        intent.putExtra("vehicle_value", "");
//        intent.putExtra("access_ip", GPSUtil.getLocalIpAddress());
//        intent.putExtra("source", 2);
//        intent.putExtra("content", "贷款买车");
//        intent.putExtra("loadType","post");
//        intent.putExtra("city", Utils.getCityName(getApplicationContext()));
//        startActivity(intent);
//    }


    @Override
    public void onDataJsonError(String errorMessage) {
        Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mNetDataJson != null) {
            mNetDataJson.cancelTask();
            mNetDataJson = null;
        }
    }

    @Override
    public void onDataJsonUpdate(String json) {
        Gson gson = new Gson();
        LoabHistoryBean orderRecord = gson.fromJson(json, LoabHistoryBean.class);

        List<LoabHistoryBean.DataBean> mRecords = new ArrayList<>();
        if(orderRecord != null &&orderRecord.getData()!= null){
            mRecords = orderRecord.getData();
        }
        if (0 == mRecords.size()) {
            nodataImageView.setVisibility(View.VISIBLE);
        }
        mLoanRecordAdapter.setData(mRecords);
    }

    private void getLoanStatus() {
        showProgressDialog();
        NetDataJson netDataJson = new NetDataJson(new NetDataJson.OnStringListener() {
            @Override
            public void onDataJsonError(String errorMessage) {
                hidenProgressDialog();
                Toast.makeText(mySelf, errorMessage, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDataJsonUpdate(String json) {
                hidenProgressDialog();
                try {
                    JSONObject jo = new JSONObject(json);
                    if (jo == null) {
                        return;
                    }
                    if ("error".equals(jo.getString("status"))) {
                        Toast.makeText(mySelf, jo.getString("msg"), Toast.LENGTH_SHORT).show();
                        return;
                    }
                    LoanUtil.toLoanData(jo.toString());
                    LoanUtil.goToPerfect(mySelf);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, 1);
        netDataJson.setUrl(NetDataJson.LOAN_SERVER_DOMAIN + API.campaigncars_evaluations);
        netDataJson.setUseOtherDomain(true);
        netDataJson.request("get");
    }
}
